package com.erp.module.zhichengJianyan.XXInspection

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.AdapterView
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.activity.DanjianivActivity
import com.erp.bean.ProcessInspectionJieHaoBean
import com.erp.bean.ZhiLiangBean
import com.erp.mclass.QuanXian
import com.erp.mclass.UnCeHandler
import com.erp.module.zhiLiang.xingJianZhiLiang.XJZhiLiangKP
import com.erp.module.zhichengJianyan.ProcessInspectionContract
import com.erp.myAdapter.InspectionphotoAdapter
import com.erp.util.Dialogutil
import com.erp.util.Httputil
import com.erp.util.Myutil

import kotlinx.android.synthetic.main.activity_xx_inspection.*
import kotlinx.android.synthetic.main.header_knoya.*

class XxInspectionActivity : BaseActivity(),ProcessInspectionContract.XxInpectionView, AdapterView.OnItemClickListener, View.OnClickListener, AdapterView.OnItemLongClickListener {


    lateinit var P : IXxInspection
    lateinit var inspectionbean: ProcessInspectionJieHaoBean
    lateinit var adapter : InspectionphotoAdapter
    lateinit var updatedialog: AlertDialog
    lateinit var deletephoto: MutableList<ProcessInspectionJieHaoBean.photo>
    var isgai = false
    override fun getLayoutId(): Int {
        return R.layout.activity_xx_inspection
    }

    override fun initCreate() {
        inspectionbean = ProcessInspectionJieHaoBean()
        inspectionbean.inspection_id = intent.getStringExtra("id")
        inspectionbean.batch = intent.getStringExtra("batch")
        deletephoto = ArrayList()
        P = IXxInspection(this,this,inspectionbean)
        adapter = InspectionphotoAdapter(inspectionbean.listphoto,this)
        GvInspectionPhoto.adapter = adapter
        if(inspectionbean.inspection_id != "") {
            updatedialog = Myutil.showupdatedialog(this@XxInspectionActivity, "正在获取", false)
            P.getInpectionBean(inspectionbean.inspection_id)
            BtQueding.text = "修改检验"
            BtDelete.visibility = View.VISIBLE
        }else{
            BtDelete.visibility = View.GONE
            BtQueding.text = "确认检验"
        }
        BtDelete.setOnClickListener(this)
        GvInspectionPhoto.onItemClickListener = this
        GvInspectionPhoto.onItemLongClickListener = this
        BtQueding.setOnClickListener(this)
        iv_header_left.setOnClickListener(this)
        iv_header_left.visibility = View.VISIBLE
        tv_header_title.text = inspectionbean.batch
        adapter.isadd(true)
    }

    fun initdata(){
        Myutil.settv(inspectionbean.batch,TvBatch)
        Myutil.settv(Myutil.getdata(inspectionbean.ctime),TvBatchCtime)
        Myutil.settv(Myutil.getdata(inspectionbean.process_inspection_time),TvInspectionCtime)
        Myutil.settv(Myutil.getdata(inspectionbean.plan_warehousing_time),TvPlanWarehousingTime)
        Myutil.settv(inspectionbean.process_inspection_name,TvInspectionName)
        adapter.notifyDataSetChanged()
        if(inspectionbean.inspection_id == ""){
            BtDelete.visibility = View.GONE
            BtQueding.text = "确认检验"
        }else{
            BtDelete.visibility = View.VISIBLE
            BtQueding.text = "修改检验"
        }
        if(!QuanXian.isEditProcessInspection){
            BtDelete.visibility = View.GONE
        }
    }

    override fun getInpectionOk() {
        initdata()
        updatedialog.dismiss()
    }

    override fun getInpectionNo() {
        updatedialog.dismiss()
    }

    override fun deleteInpectionOk() {
        inspectionbean.inspection_id = ""
        inspectionbean.inspection = ""
        inspectionbean.ctime = ""
        inspectionbean.process_inspection_name = ""
        inspectionbean.process_inspection_time = ""
        inspectionbean.listphoto.clear()
        isgai = true
        initdata()
        updatedialog.dismiss()
    }

    override fun deleteInpectionNo() {
        updatedialog.dismiss()
    }

    override fun addInpectionOk() {
        Log.d("TAAAG",inspectionbean.toString())
        isgai = true
        initdata()
        updatedialog.dismiss()
    }

    override fun addInpectionNo() {
        updatedialog.dismiss()
    }

    override fun onClick(v: View?) {
        when(v){
            BtDelete -> {
                updatedialog = Myutil.showupdatedialog(this@XxInspectionActivity, "正在取消检验", false)
                P.deleteInpection(inspectionbean.inspection_id)
            }
            BtQueding -> {
                if(inspectionbean.listphoto.size == 0){
                    val dialog = AlertDialog.Builder(this@XxInspectionActivity)
                    dialog.setTitle("提示")
                    dialog.setMessage("检验照片不能为空")
                    dialog.setPositiveButton("确定") { dialog, which ->
                        dialog.dismiss()
                    }
                    dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                    dialog.show()
                }else{
                    updatedialog = Myutil.showupdatedialog(this@XxInspectionActivity, "正在提交", false)
                    P.addInpection(inspectionbean,deletephoto)
                }
            }
            iv_header_left -> {
                if(isgai) {
                    val intent = Intent()
                    setResult(Activity.RESULT_OK, intent)
                }
                finish()
            }
        }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        UnCeHandler.buzhou="onItemClick"
        if (position == inspectionbean.listphoto.size) {
            Dialogutil.photodialog(this@XxInspectionActivity, null)
        } else {
            val intent = Intent(this@XxInspectionActivity, DanjianivActivity::class.java)
            intent.putExtra("url", inspectionbean.listphoto[position].uri)
            startActivity(intent)
        }
    }

    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        val dialog = AlertDialog.Builder(this@XxInspectionActivity)
        dialog.setTitle("提示")
        dialog.setMessage("确定要删除这个照片")
        dialog.setPositiveButton("确定") { dialog, which ->
            deletephoto.add(inspectionbean.listphoto[position])
            inspectionbean.listphoto.remove(inspectionbean.listphoto[position])
            adapter.notifyDataSetChanged()
        }
        dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
        dialog.show()
        return true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        UnCeHandler.buzhou="onActivityResult"
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                99 -> {
                    val selectedImage = data.data
                    val filePathColumns = arrayOf(MediaStore.Images.Media.DATA)
                    val c = selectedImage?.let { contentResolver.query(it, filePathColumns, null, null, null) }
                    c!!.moveToFirst()
                    val columnIndex = c.getColumnIndex(filePathColumns[0])
                    val imagePath = c.getString(columnIndex)
                    addphoto(imagePath)
                }
                100 -> {
                    val name = data.getStringExtra("dir")
                    addphoto(name)
                }
            }
        }
    }

    private fun addphoto(name: String) {
        UnCeHandler.buzhou="addphoto"
        val photo = ProcessInspectionJieHaoBean.photo()
        photo.uri = name
        inspectionbean.listphoto.add(photo)
        adapter.notifyDataSetChanged()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == 4){
            if(isgai) {
                val intent = Intent()
                setResult(Activity.RESULT_OK, intent)
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}
